import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class SortingMain {
	private static String FILENAME = "data";
	
	/** This is the constant that will be read! Changeable. */
	private static int SIZETOREAD = 10000;
	private static String METHOD = "Random";
	
	public static void main(String[] args) {
//		for(int i = 10; i <= 100000; i= i*10 ) {
//			writeFileRandom(FILENAME + i, i);
//			writeWorstCaseInsertion(FILENAME + i, i);
//			writeWorstCaseQuick(FILENAME + i, i);
//			writeBestCaseInsertion(FILENAME + i, i);
//		}
		
		Integer[] toSort = readFile(FILENAME + SIZETOREAD + METHOD);
		doSort(new InsertionSort<Integer>(), toSort, "Insertion");
		doSort(new MergeSort<Integer>(), toSort, "Merge");
		doSort(new QuickSort<Integer>(), toSort, "Quick");
	}
	
	public static void doSort(Sorter<Integer> sorter, Integer[] input, String sortType) {
		Integer[] result = sorter.sort(input, METHOD);
		sorter.writeStats(result, sortType);
	}
	
	private static Integer[] readFile(String filename) {
		ArrayList<Integer> intArrayList = new ArrayList<Integer>();
		try {
			BufferedReader file = new BufferedReader(new FileReader(filename + ".txt"));
			while( file.ready() ) {
				intArrayList.add(Integer.parseInt(file.readLine()));
			}
		} catch( FileNotFoundException fnfe ) {
			System.err.println("File not found while reading!");
		} catch( Exception e ) {
			System.err.println( "FAILED: " + e );
		}
		Integer[] toReturn = new Integer[intArrayList.size()];
		for(int i = 0; i < intArrayList.size(); i++) {
			toReturn[i] = intArrayList.get(i);
		}
		return toReturn;
	}

	private static void writeFileRandom(String filename, int size) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(filename + "Random.txt"));
			for(int i = 0; i < size; i++) {
				writer.write("" + (int) ((Math.random() * size) +1));
				if(i < size -1) {
					writer.newLine();
				}
			}
			writer.close();
		}  catch( Exception e ) {
			System.err.println( "FAILED: " + e );
		}
	}
	
	private static void writeWorstCaseInsertion(String filename, int size) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(filename + "worstcaseINSERTION.txt"));
			for(int i = size; i > 0; i--) {
				writer.write("" + i);
				if(i > 1) {
					writer.newLine();
				}
			}
			writer.close();
		}  catch( Exception e ) {
			System.err.println( "FAILED: " + e );
		}
	}
	
	private static void writeWorstCaseQuick(String filename, int size) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(filename + "worstcaseQUICK.txt"));
			for(int i = size; i > 0; i--) {
				writer.write("" + 5);
				if(i > 1) {
					writer.newLine();
				}
			}
			writer.close();
		}  catch( Exception e ) {
			System.err.println( "FAILED: " + e );
		}
	}
	
	private static void writeBestCaseInsertion(String filename, int size) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(filename + "bestcaseINSERTION.txt"));
			for(int i = 0; i < size; i++) {
				writer.write("" + i);
				if(i < size -1) {
					writer.newLine();
				}
			}
			writer.close();
		}  catch( Exception e ) {
			System.err.println( "FAILED: " + e );
		}
	}
}
